﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shl01.Models
{
    public class WarnBLLModel
    {


        public string TypeName { get; set; }//表名
        public string PrimaryName { get; set; }//主键名
        public string DBName { get; set; }//数据库名

        public Dictionary<string, string> dicType { get; set; }
        public string GetStr()
        {
            string s = "";
            foreach (KeyValuePair<string, string> keyValuePair in dicType)
            {
                if (keyValuePair.Key != "id")
                {
                    s += "g." + keyValuePair.Key + " = m." + keyValuePair.Key + " == null ? g." + keyValuePair.Key + " : m." + keyValuePair.Key + ";\n";
                }
            }
            string str = "";
            str = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using " + DBName + @".IBLL;
using " + DBName + @".IDAL;
using " + DBName + @".Model;

namespace " + DBName + @".BLL
{
    public class " + TypeName + @"Manager : BaseManager<" + TypeName + @">,I" + TypeName + @"Manager
    {
        private I" + TypeName + @"Service d = DALContainer.Container.Resolve<I" + TypeName + @"Service>();
        public override void SetDal()
        {
            Dal = d;
        }
        
        public Warn Update" + TypeName + @"(" + TypeName + @" m)
        {
            Warn w = new Warn();
            try
            {
                if (d.GetModels(d => d.del != true && d.id == m.id).Any())
                {
                    " + TypeName + @" g = d.GetModels(d => d.del != true && d.id == m.id).ToList()[0];
                    " + s + @"
                    g.xgsj = DateTime.Now;
                    d.Update(g);
                    w.Total = d.SaveChanges().ToString();
                }
                else
                {
                    w.Total = '0';
                }
    }
            catch (Exception e)
            {
                w.Error = e.Message;
            }
            return w;
        }

        public Warn Delete" + TypeName + @"(int id)
        {
            Warn w = new Warn();
            try
            {
                if (d.GetModels(d => d.del != true && d.id == id).Any())
                {
                    " + TypeName + @" g = d.GetModels(d => d.del != true && d.id == id).ToList()[0];
                    g.del = true;
                    g.scsj = DateTime.Now;
                    d.Update(g);
                    w.Total = d.SaveChanges().ToString();
                }
                else
                {
                    w.Total = '0';
                }
            }
            catch (Exception e)
            {
                w.Error = e.Message;
            }
            return w;
        }

        public Warn Add" + TypeName + @"(" + TypeName + @" m)
        {
            Warn w = new Warn();
            try
            {
                Model.gyzycs cs = null;
                if (gyzycs.GetModels(g => g.jsm == '" + TypeName + @"_bh').Any())
                {
                    cs = gyzycs.GetModels(g => g.jsm == '" + TypeName + @"_bh').ToList()[0];
                }

                if (cs == null)
                {
                    m.bh = '0';
                    cs = new gyzycs()
                    {
                        cs = '0',
                        jsm = '" + TypeName + @"_bh'
                    };
                    gyzycs.Add(cs);
                }
                else
                {
                    cs.cs = (Convert.ToInt32(cs.cs) + 1).ToString();
                    gyzycs.Update(cs);
                    m.bh = cs.cs;
                }

                m.del = false;
                m.tjsj = DateTime.Now;
                d.Add(m);
                w.Total = d.SaveChanges().ToString();
            }
            catch (Exception e)
            {
                w.Error = e.Message;
            }
            return w;
        }

        public Warn DeleteAll(List<" + TypeName + @"> ms)
        {
            Warn w = new Warn();
            try
            {
                foreach (" + TypeName + @" g in ms)
                {
                    if (d.GetModels(d => d.del != true && d.id == g.id).Any())
                    {
                        " + TypeName + @" m = d.GetModels(d => d.del != true && d.id == g.id).ToList()[0];
                        m.del = true;
                        m.scsj = DateTime.Now;
                        d.Update(m);
                    }
                }
                w.Total = d.SaveChanges().ToString();
            }
            catch (Exception e)
            {
                w.Error = e.Message;
            }

            return w;
        }
    }
}
";
            str = str.Replace("Grade", TypeName);
            return str.Replace("'", "\"");
        }
    }
}
